/*
 * @Description:
 * @Version: 2.0
 * @Autor: wsh
 * @Date: 2023-06-20 16:06:20
 * @LastEditors: uphldjx 1373577124@qq.com
 * @LastEditTime: 2024-05-07 11:34:09
 */
import { uploadType,regularRule } from '@/utils/util'
import apiMod from '@/apiMod/index'

export const importConfig = {
	tabSteps: [
		{
			label: '下载模板',
			download: '下载模板',
			active: 1,
			rules: [
				{
					label: '下载最新导入模板。共1个：',
					children: [
						{
							label: '《老人入院管理》',
							link: true,
							url: 'view_dpElderlyCheckinManage_downloadTemplate',
						},
					],
				},
			],
		},
		{
			label: '导入信息',
			description: '',
			uploadTip: '老人入院管理',
			active: 2,
			url: 'view_dpElderlyCheckinManage_import',
			code: 3207,
			rules: [
				{
					label: '导入规则说明:',
					children: [
						{
							label: '(1)导入模式为“覆盖导入”，待导入的信息会覆盖平台已存在的',
						},
					],
				},
			],
		},
	],
	dataType: 9,
}

// 列表
const tableConfig = {
	stripe: true,
	border: true,
	maxHeight: '80vh',
	columns: [
		{ label: '养老机构', prop: 'organizationName' },
		{ label: '老人姓名', prop: 'oldmanName' },
		{ label: '老人身份证号', prop: 'idCard' },
		{ label: '联系方式', prop: 'oldmanPhone' },
		{ label: '入住楼栋楼层房间床位', prop: 'buildInfoText', width: 220 },
		{ label: '入院时间', prop: 'checkinDate' },
		{ label: '离院时间', prop: 'checkoutDate' },
		{ label: '入住类型', prop: 'checkinTypeLabel' },
		{
			label: '状态',
			prop: 'status',
			custome: 'tag',
			dicData: [
				{ label: '待入院', value: 0, style: { color: '#FF8800', background: '#FAEEE0' } },
				{ label: '在院', value: 1, style: { color: '#06C270', background: '#E3F7EE' } },
				{ label: '离院中', value: 2, style: { color: '#E50000', background: '#FBE3E3' } },
				{ label: '已退院', value: 3, style: { color: '#999999', background: '#E0E0E0' } },
			],
		},
		{
			label: '操作',
			prop: 'menu',
			width: 300,
			fixed: 'right',
		},
	],
}

// 检测入院老人是否已经办理了入院
export const checkInByIdCard = async (idCard: string | number) => {
	let res = await apiMod.view_dpElderlyCheckinManage_page({ idCard, status: 1 })
	const total = res.data.total
	if (total > 0) {
		ElMessage({
			message: '老人是在院状态，不要重复办理，请核实老人具体情况',
			grouping: true,
			showClose: true,
			type: 'warning',
		})
	}
	return res.data
}

export const formConfig = () => {
	return {
		inline: false,
		showBtn: true,
		labelPosition: 'top',
		column: [
			{
				type: 'DividerHtml',
				labelPosition: 'top',
				position: 'center',
				modelValue: '老人信息',
				span: 24,
			},
			{
				label: '老人姓名',
				prop: 'idCard',
				propName: 'oldmanName',
				type: 'autoSelect',
				api: 'view_dpServiceObject_page',
				filterKey: 'idCard',
				filterLabel: 'name',
				editDisabled: true,
				paramsObj: {
					seniorLiving: 2,
				},
				multiple: false,
				rules: [{ required: true, message: '请输入老人姓名' }],
				span: 8,
				change: (item, form, value) => {
					if (item) {
						if (item.contactPhone) form.folkPhone = item.contactPhone
						if (item.contactName) form.folkName = item.contactName
						form.checkinAgreement = item.material
					} else {
						form.contactPhone = ''
						form.contactName = ''
						form.checkinAgreement = null
					}
					if (item?.telephone) form.oldmanPhone = item?.telephone
					if (item?.idCard && item.sourceType !== 'init') {
						setTimeout(() => {
							checkInByIdCard(item?.idCard)
						}, 100)
					}
				},
			},
			{
				label: '身份证号',
				prop: 'idCard',
				disabled: true,
				type: 'text',
				span: 8,
			},
			{
				label: '联系方式',
				prop: 'oldmanPhone',
				type: 'text',
				span: 8,
			},
			{
				label: '家属姓名',
				prop: 'folkName',
				type: 'input',
				span: 8,
			},
			{
				label: '家属电话',
				prop: 'folkPhone',
				type: 'text',
				span: 8,
			},
			{
				label: '家属身份证号',
				prop: 'folkIdCard',
				type: 'text',
				rules:[
					{pattern:regularRule.idCard, message: '请输入正确的家属身份证号'}
				],
				span: 8,
			},
			{
				type: 'DividerHtml',
				labelPosition: 'top',
				position: 'center',
				modelValue: '入院信息',
				span: 24,
			},
			{
				label: '入住养老机构',
				prop: 'creditCode',
				propName: 'organizationName',
				type: 'autoSelect',
				api: 'view_dpServiceOrgBase_page',
				filterKey: 'creditCode',
				filterLabel: 'organizationName',
				tipLabel: 'creditCode',
				multiple: false,
				changeField: true,
				span: 8,
				rules: [{ required: true, message: '请选择入住养老机构' }],
			},
			{
				label: '房间床位',
				prop: 'buildInfo',
				propName: 'buildInfoText',
				type: 'BuildRoomBed',
				deepLevel: 3,
				span: 8,
				rules: [{ required: true, message: '请选择房间床位' }],
			},
			{
				label: '入院时间',
				prop: 'checkinDate',
				type: 'date',
				format: 'YYYY年MM月DD日',
				valueFormat: 'YYYY-MM-DD',
				changeField: true,
				span: 8,
				rules: [{ required: true, message: '请选择入院时间' }],
			},
			{
				label: '入住类型',
				prop: 'checkinType',
				propName: 'checkinTypeLabel',
				type: 'remoteSelect',
				code: 'CHECKIN_TYPE',
				api: 'getDictItemList',
				filterKey: 'value',
				multiple: false,
				span: 8,
				rules: [{ required: true, message: '请选择入住类型' }],
			},
			{
				type: 'DividerHtml',
				labelPosition: 'top',
				position: 'center',
				modelValue: '缴费信息',
				span: 24,
			},
			{
				label: '入院缴费金额',
				prop: 'checkinAmount',
				type: 'text',
				span: 8,
			},
			{
				label: '床位费',
				prop: 'checkinBedAmount',
				type: 'text',
				span: 8,
			},
			{
				label: '护理费',
				prop: 'checkinNursingAmount',
				type: 'text',
				span: 8,
			},
			{
				label: '押金',
				prop: 'depositAmount',
				type: 'text',
				span: 8,
			},
			{
				label: '医疗保证金',
				prop: 'medicalEarnestAmount',
				type: 'text',
				span: 8,
			},
			{
				label: '餐费',
				prop: 'checkinFoodAmount',
				type: 'text',
				span: 8,
			},
			{
				type: 'DividerHtml',
				labelPosition: 'top',
				position: 'center',
				modelValue: '入住协议',
				span: 24,
			},
			{
				prop: 'checkinAgreement',
				type: 'upload',
				fileType: uploadType.fileMetaType,
				listType: 'picture-card',
				disabled: true,
				labelPosition: 'top',
				showFileList: true,
				limit: 9,
				multiple: true,
				span: 24,
				rules: [{ required: true, message: '请上传入住协议' }],
			},
		],
	}
}
export const leaveFormConfig = (data?: any) => {
	if (!data) data = {}
	return {
		inline: false,
		showBtn: true,
		labelPosition: 'top',
		column: [
			{
				label: '离院日期',
				prop: 'checkoutDate',
				type: 'date',
				format: 'YYYY年MM月DD日',
				valueFormat: 'YYYY-MM-DD',
				changeField: true,
				span: 8,
				rules: [{ required: true, message: '请选择离院日期' }],
			},
			{
				label: '回院日期',
				prop: 'returnTime',
				type: 'date',
				format: 'YYYY年MM月DD日',
				valueFormat: 'YYYY-MM-DD',
				changeField: true,
				span: 8,
				rules: [{ required: true, message: '请选择回院日期' }],
			},
			{
				label: '离院联系人',
				prop: 'familyPhone',
				propName: 'familyName',
				type: 'remoteSelect',
				api: 'view_dpServiceObjectContact_page',
				filterKey: 'contactTelephone',
				filterLabel: 'contactName',
				multiple: false,
				paramsObj: {
					idCard: data.idCard,
				},
				rules: [{ required: true, message: '请输入离院联系人' }],
				change: (item, form, value) => {
					form.elderlyRelationship = item.elderlyRelationship
					form.familyIdCard = item.contactCardId
					form.elderlyRelationshipLabel = item.elderlyRelationshipLabel
				},
				span: 8,
			},
			{
				label: '联系人电话',
				prop: 'familyPhone',
				type: 'text',
				span: 8,
			},
			{
				label: '与老人关系',
				prop: 'elderlyRelationship',
				propName: 'elderlyRelationshipLabel',
				type: 'remoteSelect',
				code: 'ELDERLY_RELATIONSHIP',
				api: 'getDictItemList',
				filterKey: 'value',
				multiple: false,
				span: 8,
				rules: [{ required: true, message: '请选择与老人关系' }],
			},
			{
				label: '离院原因',
				prop: 'checkoutReason',
				propName: 'checkoutReasonLabel',
				type: 'remoteSelect',
				code: 'CHECKOUT_REASON',
				api: 'getDictItemList',
				filterKey: 'value',
				multiple: false,
				span: 8,
			},
			{
				label: '离院说明',
				prop: 'checkLeaveIllustrate',
				type: 'textarea',
				span: 24,
			},
			{
				label: '文件',
				prop: 'checkLeaveFile',
				type: 'upload',
				fileType: uploadType.fileMetaType,
				listType: 'text',
				labelPosition: 'top',
				showFileList: true,
				limit: 9,
				multiple: true,
				span: 24,
			},
		],
	}
}
export const outFormConfig = (data?: any) => {
	if (!data) data = {}
	return {
		inline: false,
		showBtn: true,
		labelPosition: 'top',
		column: [
			{
				label: '退院时间',
				prop: 'checkoutDate',
				type: 'date',
				format: 'YYYY年MM月DD日',
				valueFormat: 'YYYY-MM-DD',
				changeField: true,
				span: 8,
				rules: [{ required: true, message: '请选择离院日期' }],
			},
			{
				label: '退院联系人',
				prop: 'folkName',
				rules: [{ required: true, message: '请输入退院联系人' }],
				span: 8,
			},
			{
				label: '联系人电话',
				prop: 'folkPhone',
				type: 'text',
				span: 8,
			},
			{
				label: '与老人关系',
				prop: 'elderlyRelationship',
				type: 'remoteSelect',
				code: 'ELDERLY_RELATIONSHIP',
				api: 'getDictItemList',
				filterKey: 'value',
				multiple: false,
				span: 8,
				rules: [{ required: true, message: '请选择与老人关系' }],
			},
			{
				label: '退院原因',
				prop: 'checkoutReason',
				propName: 'checkoutReasonLabel',
				type: 'remoteSelect',
				code: 'CHECKOUT_REASON',
				api: 'getDictItemList',
				filterKey: 'label',
				multiple: false,
				span: 8,
			},
			{
				label: '退院说明',
				prop: 'checkoutNote',
				type: 'textarea',
				span: 24,
			},
			{
				label: '文件',
				prop: 'checkoutFile',
				type: 'upload',
				fileType: uploadType.fileMetaType,
				listType: 'text',
				labelPosition: 'top',
				showFileList: true,
				limit: 9,
				multiple: true,
				span: 24,
			},
		],
	}
}

export const appointFormConfig = (data?: any) => {
	return {
		inline: false,
		showBtn: true,
		labelPosition: 'top',
		column: [
			{
				type: 'DividerHtml',
				labelPosition: 'top',
				position: 'center',
				modelValue: '老人信息',
				span: 24,
			},
			{
				label: '老人姓名',
				prop: 'idCard',
				propName: 'oldmanName',
				type: 'autoSelect',
				api: 'view_dpServiceObject_page',
				filterKey: 'idCard',
				filterLabel: 'name',
				multiple: false,
				rules: [{ required: true, message: '请输入老人姓名' }],
				span: 8,
				change: (item, form, value) => {
					form.oldmanPhone = item.telephone
					checkInByIdCard(item.idCard)
				},
			},
			{
				label: '身份证号',
				prop: 'idCard',
				disabled: true,
				type: 'text',
				span: 8,
			},
			{
				label: '联系方式',
				prop: 'oldmanPhone',
				type: 'text',
				span: 8,
			},
			{
				label: '家属姓名',
				prop: 'folkName',
				type: 'text',
				span: 8,
			},
			{
				label: '家属电话',
				prop: 'folkPhone',
				type: 'text',
				span: 8,
			},
			{
				label: '家属身份证号',
				prop: 'folkIdCard',
				type: 'text',
				span: 8,
			},
			{
				type: 'DividerHtml',
				labelPosition: 'top',
				position: 'center',
				modelValue: '入院信息',
				span: 24,
			},
			{
				label: '入住养老机构',
				prop: 'creditCode',
				propName: 'organizationName',
				type: 'autoSelect',
				api: 'view_dpServiceOrgBase_page',
				filterKey: 'creditCode',
				filterLabel: 'organizationName',
				tipLabel: 'creditCode',
				multiple: false,
				changeField: true,
				span: 8,
				rules: [{ required: true, message: '请选择入住养老机构' }],
			},
			{
				label: '房间床位',
				prop: 'buildInfo',
				propName: 'buildInfoText',
				type: 'BuildRoomBed',
				deepLevel: 3,
				span: 8,
				rules: [{ required: true, message: '请选择房间床位' }],
			},
			{
				label: '入院时间',
				prop: 'checkinDate',
				type: 'date',
				format: 'YYYY年MM月DD日',
				valueFormat: 'YYYY-MM-DD',
				changeField: true,
				span: 8,
				rules: [{ required: true, message: '请选择入院时间' }],
			},
			{
				label: '入住类型',
				prop: 'checkinType',
				propName: 'checkinTypeLabel',
				type: 'remoteSelect',
				code: 'CHECKIN_TYPE',
				api: 'getDictItemList',
				filterKey: 'value',
				multiple: false,
				span: 8,
				rules: [{ required: true, message: '请选择入住类型' }],
			},
		],
	}
}
export const operateFormConfig = (type?: string, data?: any) => {
	let typeConfig = {
		leave: leaveFormConfig(data),
		checkOut: outFormConfig(data),
		appoint: appointFormConfig(data),
	}
	return typeConfig[type] || formConfig()
}
export const detailColumn = [...formConfig().column]
export const api = {
	index: 'view_dpElderlyCheckinManage_page',
	add: 'view_dpElderlyCheckinManage_checkin',
	checkOut: 'view_dpElderlyCheckinManage_checkout',
	leave: 'view_dpElderlyLeaveRecord_checkLeave',
	delete: 'view_dpElderlyCheckinManage_del',
	edit: 'view_dpElderlyCheckinManage_edit',
	import: 'view_dpElderlyCheckinManage_import',
	appoint: '',
}
export const formateBuilData = (form) => {
	let query = {
		...form,
		buildInfo: undefined,
	}
	if (form.buildInfo && form.buildInfoText) {
		query.checkinBuilding = form.buildInfo[0]
		query.checkinFloor = form.buildInfo[1]
		query.checkinRoom = form.buildInfo[2]
		query.checkinBed = form.buildInfo[3]
	}
	return query
}

export const baseConfig = {
	pageName: '老人入院管理',
	// 数据字典
	dictcodes: ['CHECKIN_TYPE', 'CHECKOUT_REASON', 'ELDERLY_RELATIONSHIP'],
	rowKey: 'id',
	tipLabel: 'oldmanName',
	addLabel: '办理入住',
	api: {
		...api,
		importLabel: '批量入住',
	},
	auth: {
		...api,
		// 批量搜索
		mulitSet: 'mulitSet',
	},
	menuAction: [
		{
			label: '预约入住',
			type: 'primary',
			operate: 'appoint',
			auth: 'view_dpElderlyCheckinRecord_page',
			show: false,
		},
	],
	// 当前行操作按钮
	menuBtn: [
		{
			label: '查看详情',
			type: 'primary',
			operate: 'detail',
			auth: api.index,
			text: true,
			show: true,
		},
		{
			label: '编辑',
			type: 'primary',
			operate: 'edit',
			condition: '(row)=>row.status !=3',
			auth: api.edit,
			text: true,
			show: true,
		},
		{
			label: '离院',
			type: 'primary',
			operate: 'leave',
			condition: '(row)=>row.status ==1',
			auth: api.leave,
			text: true,
			show: true,
		},
		{
			label: '退院',
			type: 'primary',
			operate: 'checkOut',
			auth: api.checkOut,
			condition: '(row)=>row.status ==1',
			text: true,
			show: true,
		},
		{
			label: '入院',
			type: 'primary',
			operate: 'edit',
			auth: api.add,
			condition: '(row)=>row.status ==0',
			text: true,
			show: true,
		},
	],
	dialogConfig: {
		width: '80%',
	},
	// 搜索
	searchConfig: {
		inline: true,
		showBtn: true,
		labelPosition: 'left',
		column: [
			{ label: '房间号', prop: 'checkinRoom', type: 'input', span: 6 },
			{ label: '老人姓名', prop: 'oldmanName', type: 'input', span: 6 },
			{ label: '身份证号', prop: 'idCard', type: 'input', span: 6 },
			{
				label: '入住楼栋楼层房间',
				prop: 'buildInfo',
				propName: 'buildInfoText',
				type: 'BuildRoomBed',
				deepLevel: 2,
				span: 6,
			},
			{ label: '联系方式', prop: 'oldmanPhone', type: 'input', span: 6 },
			{
				label: '入院时间',
				prop: 'checkinDate',
				type: 'date',
				format: 'YYYY年MM月DD日',
				valueFormat: 'YYYY-MM-DD',
				span: 6,
			},
			{
				label: '入住类型',
				prop: 'checkinType',
				propName: 'checkinTypeLabel',
				type: 'remoteSelect',
				code: 'CHECKIN_TYPE',
				api: 'getDictItemList',
				filterKey: 'value',
				multiple: false,
				span: 6,
			},
			{
				label: '状态',
				prop: 'status',
				type: 'select',
				multiple: false,
				dicData: [
					{ label: '待入院', value: 0 },
					{ label: '在院', value: 1 },
					{ label: '离院中', value: 2 },
					{ label: '已退院', value: 3 },
				],
				span: 6,
			},
		],
	},
	// 列表
	tableConfig,
	formConfig: formConfig(),
	importConfig,
	onLoadFormate: async (arr) => {
		const tableData = arr.map((item) => {
			return {
				...item,
				buildInfo: [item.checkinBuilding, item.checkinFloor, item.checkinRoom, item.checkinBed],
				buildInfoText: [item.checkinBuilding, item.checkinFloor, item.checkinRoom, item.checkinBed].join('/'),
			}
		})
		return Promise.resolve(tableData)
	},
	beforeOnLoadFormate(form) {
		return new Promise((resolve, reject) => {
			resolve(formateBuilData(form))
		})
	},
}
